﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using ObjectBakery.Interfaces;

namespace ObjectBakery
{
    public class ObjectProxy : IObjectProxy
    {
        private readonly Type proxiedType;
        private readonly object proxiedObject;

        public object ProxiedObject
        {
            get { return proxiedObject; }
        }

        public ObjectProxy(object instance)
        {
            this.proxiedObject = instance;
            proxiedType = instance.GetType();
        }

        public object InvokeMethod(string strMethodName, object[] parameters)
        {

            return proxiedType.InvokeMember(strMethodName,
                                              BindingFlags.InvokeMethod,
                                              null,
                                              proxiedObject,
                                              parameters);
        }

        public object GetProperty(string propertyName)
        {

            System.Reflection.PropertyInfo myProperty = proxiedObject.GetType().GetProperty(propertyName);
            return myProperty.GetValue(proxiedObject, null);

        }

        public void SetProperty(string propertyName, object value)
        {

            System.Reflection.PropertyInfo myProperty = proxiedObject.GetType().GetProperty(propertyName);
            myProperty.SetValue(proxiedObject, value, null);

        }
    }
}
